package com.wan37.logic.trading.util;

import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.logic.trading.domain.vo.TradingVO;
import com.wan37.protobuf.constant.BackpackLabel;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.constant.ShopLabel;
import com.wan37.protobuf.message.client2server.game.GameTrading;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-07-31 14:50
 **/
@Component
public class TradingRouteUtil {
    /**
     *
     * @param code
     * @param tradingVOList
     * @param senderId
     * @param receiverId
     */
    public void sendTradingResponse(int code, List<TradingVO> tradingVOList, String senderId, String receiverId){
        GameTrading.TradingResponse.Builder trb = GameTrading.TradingResponse.newBuilder();
        trb.setCode(code);
        trb.setStringId(senderId);
        if (tradingVOList != null){
            for (TradingVO tradingVO : tradingVOList){
                GameTrading.Trading.Builder tb = GameTrading.Trading.newBuilder();
                tb.setTradingId(tradingVO.getTradingId());
                tb.setSenderId(tradingVO.getSenderId());
                tb.setReceiverId(tradingVO.getReceiverId());
                tb.setType((tradingVO.getType()));
                if (tradingVO.getType() == ShopLabel.MONEY_TYPE_GOLD){
                    tb.setGold(tradingVO.getGold());
                }else if (tradingVO.getType() == BackpackLabel.WEAPONRY
                        || tradingVO.getType() == BackpackLabel.ITEM){
                    for (Map.Entry<String, Integer> entry : tradingVO.getBackpackIdToCountMap().entrySet()){
                        tb.addBackpackIdToCountMap(GameTrading.BackpackIdToCountMap.newBuilder()
                                .setBackpackId(entry.getKey()).setCount(entry.getValue()).build());
                    }
                }
                tb.setIngot(tradingVO.getIngot());
                trb.addTrading(tb.build());
            }
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                receiverId, PtoNum.TRADINGRESPONSE, trb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     *
     * @param code
     * @param senderId 发送者
     * @param receiverId 接收者
     * @param tradingVO
     */
    public void sendTradingResponse(int code, String senderId, String receiverId, TradingVO tradingVO){
        GameTrading.TradingResponse.Builder trb = GameTrading.TradingResponse.newBuilder();
        trb.setCode(code);
        trb.setStringId(senderId);
        if (tradingVO != null){
            GameTrading.Trading.Builder tb = GameTrading.Trading.newBuilder();
            tb.setTradingId(tradingVO.getTradingId());
            tb.setSenderId(tradingVO.getSenderId());
            tb.setReceiverId(tradingVO.getReceiverId());
            tb.setType((tradingVO.getType()));
            if (tradingVO.getType() == ShopLabel.MONEY_TYPE_GOLD){
                tb.setGold(tradingVO.getGold());
            }else if (tradingVO.getType() == BackpackLabel.WEAPONRY
                    || tradingVO.getType() == BackpackLabel.ITEM){
                for (Map.Entry<String, Integer> entry : tradingVO.getBackpackIdToCountMap().entrySet()){
                    tb.addBackpackIdToCountMap(GameTrading.BackpackIdToCountMap.newBuilder()
                            .setBackpackId(entry.getKey()).setCount(entry.getValue()).build());
                }
            }
            tb.setIngot(tradingVO.getIngot());
            trb.addTrading(tb.build());
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                receiverId, PtoNum.TRADINGRESPONSE, trb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendTradingResponse(int code, String receiverId, TradingVO tradingVO){
        GameTrading.TradingResponse.Builder trb = GameTrading.TradingResponse.newBuilder();
        trb.setCode(code);
        if (tradingVO != null){
            GameTrading.Trading.Builder tb = GameTrading.Trading.newBuilder();
            tb.setTradingId(tradingVO.getTradingId());
            tb.setSenderId(tradingVO.getSenderId());
            tb.setReceiverId(tradingVO.getReceiverId());
            tb.setType((tradingVO.getType()));
            if (tradingVO.getType() == ShopLabel.MONEY_TYPE_GOLD){
                tb.setGold(tradingVO.getGold());
            }else if (tradingVO.getType() == BackpackLabel.WEAPONRY
                    || tradingVO.getType() == BackpackLabel.ITEM){
                for (Map.Entry<String, Integer> entry : tradingVO.getBackpackIdToCountMap().entrySet()){
                    tb.addBackpackIdToCountMap(GameTrading.BackpackIdToCountMap.newBuilder()
                            .setBackpackId(entry.getKey()).setCount(entry.getValue()).build());
                }
            }
            tb.setIngot(tradingVO.getIngot());
            trb.addTrading(tb.build());
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                receiverId, PtoNum.TRADINGRESPONSE, trb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendTradingResponse(int code, List<TradingVO> tradingVOList, String receiverId){
        GameTrading.TradingResponse.Builder trb = GameTrading.TradingResponse.newBuilder();
        trb.setCode(code);
        if (tradingVOList != null){
            for (TradingVO tradingVO : tradingVOList){
                GameTrading.Trading.Builder tb = GameTrading.Trading.newBuilder();
                tb.setTradingId(tradingVO.getTradingId());
                tb.setSenderId(tradingVO.getSenderId());
                tb.setReceiverId(tradingVO.getReceiverId());
                tb.setType((tradingVO.getType()));
                if (tradingVO.getType() == ShopLabel.MONEY_TYPE_GOLD){
                    tb.setGold(tradingVO.getGold());
                }else if (tradingVO.getType() == BackpackLabel.WEAPONRY
                        || tradingVO.getType() == BackpackLabel.ITEM){
                    for (Map.Entry<String, Integer> entry : tradingVO.getBackpackIdToCountMap().entrySet()){
                        tb.addBackpackIdToCountMap(GameTrading.BackpackIdToCountMap.newBuilder()
                                .setBackpackId(entry.getKey()).setCount(entry.getValue()).build());
                    }
                }
                tb.setIngot(tradingVO.getIngot());
                trb.addTrading(tb.build());
            }
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                receiverId, PtoNum.TRADINGRESPONSE, trb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }
}
